layui.use(['table', 'admin', 'ax', 'laydate', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;

    laydate.render({
        elem:'#startTime'
        ,value:new Date()
    });
    laydate.render({
        elem:'#endTime'
        ,value:new Date()
    });
    /**
     * 移库记录表管理
     */
    var HxStorageMoveRecord = {
        tableId: "hxStorageMoveRecordTable"
    };

    /**
     * 初始化表格的列
     */
    HxStorageMoveRecord.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'oldPositionNumber', sort: true, title: '原库位编号'},
            {field: 'oldPositionType', sort: true, title: '原库位类型'},
            {field: 'newPositionNumber', sort: true, title: '新库位编号'},
            {field: 'newPositionType', sort: true, title: '新库位类型'},
            {field: 'trayNumber', sort: true, title: '托盘编号'},
            {field: 'goodsName', sort: true, title: '商品'},
            {field: 'goodsNumber', sort: true, title: '商品编号'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'kcUnit', sort: true, title: '单位', totalRowText: '合计：'},
            {field: 'kcNum', sort: true, title: '库存数量', totalRowText: sl.kcNum},
            {field: 'transferTime', sort: true, title: '移库时间'},
            {field: 'transferUser', sort: true, title: '操作员'},
            {field: 'returnStatus', sort: true, title: '回传状态'},
            {field: 'warehouseName', sort: true, title: '仓库名称'}

        ]];
    };

    /**
     * 点击查询按钮
     */
    HxStorageMoveRecord.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['condition'] = $("#condition").val();
        queryData['returnStatus'] = $("#storageState").val();
        queryData['customerId'] = $("#customerId").val();
        queryData['warehouseId'] = $("#warehouseId").val();
	    $.ajax({
	        url:Feng.ctxPath + '/hxStorageMoveRecord/count',
            data: queryData, // 将 queryData 作为请求体发送
	        dataType:"json",   //返回格式为json
	        async: false,
	        type:"post",   //请求方式
	        success:function(data){
	            sl = data.data
	        }
	    });
	
	    // 渲染表格
	    var tableResult = table.render({
	        elem: '#' + HxStorageMoveRecord.tableId,
            where: queryData,
	        url: Feng.ctxPath + '/hxStorageMoveRecord/list',
	        page: true,
	        limit: 20,
	        height: "full-60",
	        toolbar: '#toolbarfrom',
	        totalRow: true ,//开启合计行
	        cellMinWidth: 100,
	        cols: HxStorageMoveRecord.initColumn()
	    });
    };

    /**
     * 弹出添加对话框
     */
    HxStorageMoveRecord.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/hxStorageMoveRecord/add';
    };

    /**
     * 导出excel按钮
     */
    HxStorageMoveRecord.exportExcel = function () {
        var checkRows = table.checkStatus(HxStorageMoveRecord.tableId);
        if (checkRows.data.length > 0) {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        } else {
	        var queryData = {};
	        queryData['startTime'] = $("#startTime").val();
	        queryData['endTime'] = $("#endTime").val();
	        queryData['condition'] = $("#condition").val();
	        queryData['returnStatus'] = $("#storageState").val();
            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/hxStorageMoveRecord/listAll",
                dataType:'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                    layer.close(index);
                },
                error: function (data) {
                    console.log(data)
                }
            });
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    HxStorageMoveRecord.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/hxStorageMoveRecord/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    HxStorageMoveRecord.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/hxStorageMoveRecord/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(HxStorageMoveRecord.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    var sl = {
        kcNum:""
    }

    $.ajax({
        url:Feng.ctxPath + '/hxStorageMoveRecord/count',
        dataType:"json",   //返回格式为json
        async: false,
        type:"post",   //请求方式
        success:function(data){
            sl = data.data
        }
    });

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + HxStorageMoveRecord.tableId,
        url: Feng.ctxPath + '/hxStorageMoveRecord/list',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        totalRow: true ,//开启合计行
        cellMinWidth: 100,
        cols: HxStorageMoveRecord.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        HxStorageMoveRecord.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        HxStorageMoveRecord.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        HxStorageMoveRecord.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + HxStorageMoveRecord.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            HxStorageMoveRecord.openEditDlg(data);
        } else if (layEvent === 'delete') {
            HxStorageMoveRecord.onDeleteItem(data);
        }
    });
});
